<script type="text/javascript">
    $(document).ready(function(){
        $.ajax({
            url: '{{$jqgrid.url_metadata}}',
            type: 'GET',
            dataType: 'json',
            error: function(){
                alert('Can not load init jqrip!');
            },
            success: function(data){//console.log(data);
                $("#{{$jqgrid.id}}").jqGrid({ 
                    url: '{{$jqgrid.url_data}}',
                    datatype: "json",
                    mtype: "POST",
                    colNames: data.names,
                    colModel: data.model,
                    height:'auto',
                    autowidth: true,
                    rowNum:20,
                    rowList:[20,30,50],
                    pager: '#{{$jqgrid.pager}}',
                    caption: "{{$jqgrid.caption}}",
                    viewrecords: true,
                    multiselect: true,
                    sortable: true
                });
                jQuery("#{{$jqgrid.id}}").jqGrid(
                    'navGrid',
                    '#{{$jqgrid.pager}}', 
                    {view:true, add:false, edit:false, del:false, search:true, refresh: true}, //options
                    {}, // edit options 
                    {}, // add options 
                    {}, // del options 
                    {height:'auto',closeAfterSearch:true,multipleSearch:true} // search options 
                ).navButtonAdd('#{{$jqgrid.pager}}',{
                    caption:"{{$language->line('create')}}", 
                    buttonicon:"ui-icon-add", 
                    onClickButton: function(){ 
                        urlGo('{{$jqgrid.url_create}}/');
                    }, 
                    position:"last"
                }).navButtonAdd('#{{$jqgrid.pager}}',{
                    caption:"{{$language->line('edit')}}", 
                    buttonicon:"ui-icon-edit", 
                    onClickButton: function(){ 
                        var gr = $("#{{$jqgrid.id}}").jqGrid('getGridParam', 'selarrrow');
                        if( gr.length != 0 )
                            if(gr.length>1)
                                alert('Chỉ được chọn một dòng để sửa!');
                            else
                                urlGo('{{$jqgrid.url_edit}}/id/'+gr);
                        else alert("{{$language->line('please_select')}}");
                    }, 
                    position:"last"
                }).navButtonAdd('#{{$jqgrid.pager}}',{
                    caption:"{{$language->line('delete')}}", 
                    buttonicon:"ui-icon-delete", 
                    onClickButton: function(){ 
                        var gr = $("#{{$jqgrid.id}}").jqGrid('getGridParam', 'selarrrow');
                        //console.log(gr);
                        if( gr.length != 0 ){
                            var ids = '';
                            for(var i=0; i<gr.length; i++)
                            {
                                if(i < gr.length-1)
                                    ids+= gr[i]+'-';
                                else
                                    ids+= gr[i];      
                            }
                            confirmUrl("{{$language->line('delete_confirm')}}", '{{$jqgrid.url_delete}}/id/'+ids);
                            //alert('{{$jqgrid.url_delete}}/id/'+ids);
                        }
                        else alert("{{$language->line('please_select')}}");
                    }, 
                    position:"last"
                }).navButtonAdd('#{{$jqgrid.pager}}',{
                    caption:"Mới", 
                    buttonicon:"ui-icon-add", 
                    onClickButton: function(){ 
                        var gr = $("#{{$jqgrid.id}}").jqGrid('getGridParam', 'selarrrow');
                        //console.log(gr);
                        if( gr.length != 0 ){
                            var ids = '';
                            for(var i=0; i<gr.length; i++)
                            {
                                if(i < gr.length-1)
                                    ids+= gr[i]+'-';
                                else
                                    ids+= gr[i];      
                            }
                            urlGo('{{$jqgrid.url_add_newhot}}/type/1/id/'+ids);//type=1 (thêm sp mới)
                        }
                        else alert("{{$language->line('please_select')}}");
                    },
                    position:"last"
                }).navButtonAdd('#{{$jqgrid.pager}}',{
                    caption:"Hot", 
                    buttonicon:"ui-icon-add", 
                    onClickButton: function(){ 
                        var gr = $("#{{$jqgrid.id}}").jqGrid('getGridParam', 'selarrrow');
                        //console.log(gr);
                        if( gr.length != 0 ){
                            var ids = '';
                            for(var i=0; i<gr.length; i++)
                            {
                                if(i < gr.length-1)
                                    ids+= gr[i]+'-';
                                else
                                    ids+= gr[i];      
                            }
                            urlGo('{{$jqgrid.url_add_newhot}}/type/2/id/'+ids);//type=2 (thêm sp bán chạy)
                        }
                        else alert("{{$language->line('please_select')}}");
                    },
                    position:"last"
                });
                jQuery("#{{$jqgrid.id}}").jqGrid('filterToolbar',{stringResult: true,searchOnEnter : false});
            }
        });
    });
</script>